var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];
web.user_list=(function(){
    return function(page,current,obj,scrollLoad){
        var user_data = {
            DEPARTMENT_ID: 0
        }
        var succCallBack = function (data, status, response) {
            if(data.varList.length == 0){
                $("#operateBtn").css({
                    display: "none"
                })
            }
            if(data.varList != ""){
                department_two( data.varList[0].DEPARTMENT_ID,data.varList[0].BIANMA,data.varList[0].DEPARTMENT_NAME);
                $(".removeEl").remove();//清空分组
                for(var i=0;i<data.varList.length;i++){
                    $("#departmentName").append('<li class="removeEl" title="'+data.varList[i].DEPARTMENT_NAME+'\n'+data.varList[i].BIANMA+'" onclick="department_two(\''+ data.varList[i].DEPARTMENT_ID+ '\',\''+ data.varList[i].BIANMA+ '\',\''+ data.varList[i].DEPARTMENT_NAME+ '\')" style="padding-bottom: 2px;width: 215px;height:22px;overflow: hidden;">' +'<a class="department_name">'+data.varList[i].DEPARTMENT_NAME+'</a></li>');
                }
            }
        };
        return web.doAjax(root.accessInter.access_department_listApi,'post',user_data, succCallBack);
    }
    power();
})();

//编写随机函数返回菜单的唯一标志用来删除菜单路径
function creatName(n) {
    var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
    var res = "";
    for(var i = 0; i < n; i++) {
        var id = Math.ceil(Math.random() * 35);
        res += chars[id];
    }
    return res;
}

// 切换到二级部门
function department_two(id,code,depName) {
    $(".a_remove").remove();
    $(".depTitle strong").html(code+"&nbsp;"+depName);
    $(".secondDep").remove();
    var user_data = {
        DEPARTMENT_ID: id
    }
    var succCallBack = function (data, status, response) {
        console.log(data)
        $(".secondRemove").remove();//清空分组
        var editData = {
            DEPARTMENT_ID:data.pd.DEPARTMENT_ID,
            DEPARTMENT_NAME:data.pd.DEPARTMENT_NAME,
            P_BIANMA:data.pd.P_BIANMA,
            MAX_NUM:data.pd.MAX_NUM,
            PARENT_ID:data.pd.PARENT_ID
        };
        window.session_storage.setItem('delDep',JSON.stringify(editData));
        for(var i=0;i<data.varList.length;i++){
            $(".nextDep").after('<a class="a_remove"><div title="'+data.varList[i].DEPARTMENT_NAME+'\n'+data.varList[i].BIANMA+'" class="pl-20 pt-10 pb-25 secondRemove" onclick="department_next(\''+ data.varList[i].DEPARTMENT_ID+ '\',\''+ data.varList[i].BIANMA+ '\',\''+ data.varList[i].DEPARTMENT_NAME+ '\')" ' + 'style="padding-bottom: 2px;width: 215px;height:22px;overflow: hidden;">' + '<span class="department_name">'+data.varList[i].DEPARTMENT_NAME+'</span></div></a>');
        }
        var className = creatName(4);
        $("#depMenu").append('<span class="secondDep" id="'+className+'"><a onclick="lastDep(this,\''+ data.pd.DEPARTMENT_ID+ '\',\''+ data.pd.BIANMA+ '\',\''+ data.pd.DEPARTMENT_NAME+ '\')">'+depName+'</a></span>');

    };
    power();
    return web.doAjax(root.accessInter.access_department_listApi,'post',user_data, succCallBack);
}
// 切换到下一级部门
function department_next(id,code,depName) {
    $(".a_remove").remove();
    $(".depTitle strong").html(code+"&nbsp;"+depName);
    var user_data = {
        DEPARTMENT_ID: id
    };
    var succCallBack = function (data, status, response) {
        $(".secondRemove").remove();//清空分组
        var editData = {
            DEPARTMENT_ID:data.pd.DEPARTMENT_ID,
            DEPARTMENT_NAME:data.pd.DEPARTMENT_NAME,
            P_BIANMA:data.pd.P_BIANMA,
            MAX_NUM:data.pd.MAX_NUM,
            PARENT_ID:data.pd.PARENT_ID
        };
        window.session_storage.setItem('delDep',JSON.stringify(editData));
        for(var i=0;i<data.varList.length;i++){
            $(".nextDep").after('<a class="a_remove"><div  title="'+data.varList[i].DEPARTMENT_NAME+'\n'+data.varList[i].BIANMA+'" class="pl-20 pt-10 pb-25 secondRemove department_name1" onclick="department_next(\''+ data.varList[i].DEPARTMENT_ID+ '\',\''+ data.varList[i].BIANMA+ '\',\''+ data.varList[i].DEPARTMENT_NAME+ '\')" style="padding-bottom: 2px;width: 215px;height:22px;overflow: hidden;"><span>'+data.varList[i].DEPARTMENT_NAME+'</span></div></a>');
        }
        var className = creatName(4);
        $("#depMenu").append('<span  class="secondDep" id="'+className+'">&nbsp;&gt;&nbsp;<a onclick="lastDep(this,\''+ data.pd.DEPARTMENT_ID+ '\',\''+ data.pd.BIANMA+ '\',\''+ data.pd.DEPARTMENT_NAME+ '\')">'+depName+'</a></span>');
    };
    power();
    return web.doAjax(root.accessInter.access_department_listApi,'post',user_data, succCallBack);
}

// 返回某一级部门
function lastDep(obj,id,code,depName) {
    $(".a_remove").remove();
    $(".depTitle strong").html(code+"&nbsp;"+depName);

    $("#"+obj.parentNode.id).nextAll().remove();//删除所点击菜单后的多余菜单路径
    var user_data = {
        DEPARTMENT_ID: id
    }
    var succCallBack = function (data, status, response) {
        var editData = {
            DEPARTMENT_ID:data.pd.DEPARTMENT_ID,
            DEPARTMENT_NAME:data.pd.DEPARTMENT_NAME,
            P_BIANMA:data.pd.P_BIANMA,
            MAX_NUM:data.pd.MAX_NUM,
            PARENT_ID:data.pd.PARENT_ID
        };
        window.session_storage.setItem('delDep',JSON.stringify(editData));
        $(".secondRemove").remove();//清空分组
        for(var i=0;i<data.varList.length;i++){
            $(".nextDep").after('<a class="a_remove"><div title="'+data.varList[i].DEPARTMENT_NAME+'\n'+data.varList[i].BIANMA+'" class="pl-20 pt-10 pb-25 secondRemove" onclick="department_next(\''+ data.varList[i].DEPARTMENT_ID+ '\',\''+ data.varList[i].BIANMA+ '\',\''+ data.varList[i].DEPARTMENT_NAME+ '\')" style="padding-bottom: 2px;width: 215px;height:22px;overflow: hidden;"><span class="department_name">'+data.varList[i].DEPARTMENT_NAME+'</span></div></a>');
        }
    };
    power();
    return web.doAjax(root.accessInter.access_department_listApi,'post',user_data, succCallBack);
}

